package cn.iocoder.yudao.module.erp.controller.admin.invoiceintegration;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.invoicechecklist.vo.ApprovalVO;
import cn.iocoder.yudao.module.erp.controller.admin.invoiceintegration.vo.InvoiceIntegrationPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.invoiceintegration.vo.InvoiceIntegrationRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.invoiceintegration.vo.InvoiceIntegrationSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.invoiceintegration.InvoiceIntegrationDO;
import cn.iocoder.yudao.module.erp.service.invoiceintegration.InvoiceIntegrationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 发票整合")
@RestController
@RequestMapping("/erp/invoice-integration")
@Validated
public class InvoiceIntegrationController {

    @Resource
    private InvoiceIntegrationService invoiceIntegrationService;

    @PostMapping("/create")
    @Operation(summary = "创建发票整合")
    @PreAuthorize("@ss.hasPermission('erp:invoice-integration:create')")
    public CommonResult<String> createInvoiceIntegration(@Valid @RequestBody InvoiceIntegrationSaveReqVO createReqVO) {
        return success(invoiceIntegrationService.createInvoiceIntegration(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新发票整合")
    @PreAuthorize("@ss.hasPermission('erp:invoice-integration:update')")
    public CommonResult<Boolean> updateInvoiceIntegration(@Valid @RequestBody InvoiceIntegrationSaveReqVO updateReqVO) {
        invoiceIntegrationService.updateInvoiceIntegration(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除发票整合")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:invoice-integration:delete')")
    public CommonResult<Boolean> deleteInvoiceIntegration(@RequestParam("id") String id) {
        invoiceIntegrationService.deleteInvoiceIntegration(id);
        return success(true);
    }

    @GetMapping("/sendInvoiceIntegration")
    @Operation(summary = "获得发票整合")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:invoice-integration:query')")
    public CommonResult<Boolean> sendInvoiceIntegration(@RequestParam("id") String id) {
        invoiceIntegrationService.sendInvoiceIntegration(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得发票整合")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:invoice-integration:query')")
    public CommonResult<InvoiceIntegrationRespVO> getInvoiceIntegration(@RequestParam("id") String id) {
        InvoiceIntegrationDO invoiceIntegration = invoiceIntegrationService.getInvoiceIntegration(id);
        return success(BeanUtils.toBean(invoiceIntegration, InvoiceIntegrationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得发票整合分页")
    @PreAuthorize("@ss.hasPermission('erp:invoice-integration:query')")
    public CommonResult<PageResult<InvoiceIntegrationRespVO>> getInvoiceIntegrationPage(@Valid InvoiceIntegrationPageReqVO pageReqVO) {
        PageResult<InvoiceIntegrationRespVO> pageResult = invoiceIntegrationService.getInvoiceIntegrationPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出发票整合 Excel")
    @PreAuthorize("@ss.hasPermission('erp:invoice-integration:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInvoiceIntegrationExcel(@Valid InvoiceIntegrationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InvoiceIntegrationRespVO> list = invoiceIntegrationService.getInvoiceIntegrationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "发票整合.xls", "数据", InvoiceIntegrationRespVO.class,
                        list);
    }

    //    根据发票整合单编号 修改发票审核单状态
    @PutMapping("/update-status-by-check-invoice-id")
    @Operation(summary = "根据发票整合单id 修改发票审核单状态")
    @PreAuthorize("@ss.hasPermission('erp:invoice-integration:update')")
    public CommonResult<Boolean> updateStatusByCheckInvoiceId(ApprovalVO approvalVO) {
        invoiceIntegrationService.updateStatusByCheckInvoiceId(approvalVO.getCheckInvoiceId(), approvalVO.getInvoiceStatus(), approvalVO.getTaskId(), approvalVO.getApprovalOpinion());
        return success(true);
    }
    @PutMapping("/update-pay-status-by-check-invoice-id")
    @Operation(summary = "根据发票整合单编号 修改打款审核状态")
    @PreAuthorize("@ss.hasPermission('erp:invoice-integration:update')")
    public CommonResult<Boolean> updatePayStatusByCheckInvoiceId(@RequestParam("checkInvoiceId") String checkInvoiceId,
                                                                 @RequestParam("payStatus") String payStatus,
                                                                 @RequestParam("taskId") String taskId,
                                                                 @RequestParam("approvalOpinion") String approvalOpinion) {
        invoiceIntegrationService.updatePayStatusByCheckInvoiceId(checkInvoiceId, payStatus,taskId,approvalOpinion);
        return success(true);
    }

}